// 文件库请求相关
import request from "@/api";
import { FileInfo } from "@/type";

/**
 * 下载源文件（原图）
 * @param bucketName
 * @param fileFullName
 * @returns
 */
export const downLoadFile = <T>(bucketName: string, fileFullName: string) => {
	return request<T>({
		method: "post",
		url: "/fileApi/FileService/DownLoadObject",
		params: {
			bucketName,
			fileFullName,
		},
		responseType: "blob",
	});
};
/**
 *下载缩略图
 * @param fileId 文件全路径
 * @returns
 */
export const downLoadThumbnail = <T>(
	fileFullName: string,
	bucketName: string
) => {
	return request<T>(
		{
			method: "post",
			url: "/fileApi/FileService/DownLoadThumbnail",
			params: {
				fileFullName,
				bucketName,
			},
			responseType: "blob",
		},
		{
			isLoading: false,
			isToast: true,
		}
	);
};

/**
 * 分片上传
 * @param bucketName
 * @param fileFullName
 * @param md5
 * @param chunkIndex
 * @param totalChunk
 * @param formData
 * @returns
 */
export const uploadPart = <T>(
	bucketName: string,
	fileFullName: string,
	md5: string,
	chunkIndex: number,
	totalChunk: number,
	formData: FormData
) => {
	return request<T>(
		{
			method: "post",
			url: "/fileApi/FileService/UploadPart",
			headers: {
				"Content-Type": "multipart/form-data",
			},
			params: {
				bucketName,
				fileFullName,
				md5: md5,
				chunkIndex,
				totalChunk,
			},
			data: formData,
		},
		{
			isLoading: false,
			isToast: true,
		}
	);
};

// 文件上传
export const putObject = <T>(
	bucketName: string,
	fileFullName: string,
	formData: FormData
) => {
	return request<T>(
		{
			method: "put",
			url: "/fileApi/FileService/PutObject",
			headers: {
				"Content-Type": "multipart/form-data",
			},
			params: {
				bucketName,
				fileFullName,
			},
			data: formData,
		},
		{
			isLoading: false,
			isToast: true,
		}
	);
};

/**
 * 验证文件是否存在
 * @param bucketName
 * @param fileFullName
 * @returns
 */
export const doesFileExist = <T>(bucketName: string, fileFullName: string) => {
	return request<T>(
		{
			method: "post",
			url: "/fileApi/FileService/DoesFileExist",
			params: {
				bucketName,
				fileFullName,
			},
		},
		{
			isLoading: false,
			isToast: true,
		}
	);
};

/**
 * 文件打包下载（压缩包）
 * @param fileFullName
 * @returns
 */
export const downLoadTempObject = <T>(fileFullName: string) => {
	return request<T>({
		method: "post",
		url: "/fileApi/FileService/DownLoadTempObject",
		params: {
			fileFullName,
		},
		responseType: "blob",
	});
};

/**
 * 获取windows共享目录下pacs拉去的文件缩略图
 * @param fileFullName
 * @returns
 */
export const getPacsImage = <T>(fileFullName: string) => {
	return request<T>({
		method: "get",
		url: "/pacsApi/PacsServices/GetFile",
		params: {
			fileFullName: fileFullName,
		},
		responseType: "blob",
	});
};
